package com.mask.roll.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.exception.RollException;
import com.mask.roll.edu.entity.Subject;
import com.mask.roll.edu.entity.VO.subject.SubjectExcelVO;
import com.mask.roll.edu.entity.VO.subject.OneSubjectVO;
import com.mask.roll.edu.entity.VO.subject.TwoSubjectVO;
import com.mask.roll.edu.listener.SubjectExcelListener;
import com.mask.roll.edu.mapper.SubjectMapper;
import com.mask.roll.edu.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author hx
 * @since 2021-10-23
 */
@Service
@Slf4j
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Override
    public void saveSubject(MultipartFile file,SubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            log.info("读取文件{}开始...",file.getOriginalFilename());
            EasyExcel.read(in, SubjectExcelVO.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new RollException(ServiceEnums.EXCEL_READ_ERROR.getCode(),
                    RollConstants.ERROR,ServiceEnums.EXCEL_READ_ERROR.getMessage());
        }

    }

    @Override
    public List<OneSubjectVO> getTreeList() {
        //查询所有一级分类
        QueryWrapper<Subject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<Subject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询二级分类
        QueryWrapper<Subject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<Subject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //封装tree list
        ArrayList<OneSubjectVO> finalSubjectList = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            Subject subject = oneSubjectList.get(i);
            OneSubjectVO oneSubjectVO = new OneSubjectVO();

            //将subject的值copy到OneSubject中
            BeanUtils.copyProperties(subject, oneSubjectVO);
            finalSubjectList.add(oneSubjectVO);

            //封装二级分类
            ArrayList<TwoSubjectVO> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                Subject twoSubject = twoSubjectList.get(j);
                //找出一级分类下的所有二级分类
                if (twoSubject.getParentId().equals(subject.getId())){
                    TwoSubjectVO twoSubjectVO2 = new TwoSubjectVO();
                    BeanUtils.copyProperties(twoSubject, twoSubjectVO2);
                    twoFinalSubjectList.add(twoSubjectVO2);
                }
            }
            oneSubjectVO.setChildren(twoFinalSubjectList);
        }

        return finalSubjectList;
    }
}
